package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.StuCertificationMapper;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.StuCertification;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.IStuCertificationService;
import com.farmpost.server.utils.Files;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.HashMap;

/**
 * <p>
 * 学生认证表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-03
 */
@Service
public class StuCertificationServiceImpl extends ServiceImpl<StuCertificationMapper, StuCertification> implements IStuCertificationService {

    @Autowired
    private StuCertificationMapper stuCertificationMapper;
    @Autowired
    private Files files;

    /**
     * 学生认证信息插入
     *
     * @param stuId
     * @param userId
     * @param stuProfession
     * @param stuSchool
     * @param stuGraduateTime
     * @return
     */
    @Override
    public RespBean insertStu(Integer stuId, Integer userId, String stuProfession, String stuSchool, LocalDate stuGraduateTime) {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (userId == null) {
            userId = user.getUserId();
        } else if (user.getUserId() != (int) userId) {
            return RespBean.error("当前用户与认证用户不匹配");
        }
        StuCertification stuCertification = new StuCertification();
        stuCertification.setStuId(stuId);
        stuCertification.setUserId(userId);
        stuCertification.setStuProfession(stuProfession);
        stuCertification.setStuSchool(stuSchool);
        stuCertification.setStuGraduateTime(stuGraduateTime);
        int insert = stuCertificationMapper.insert(stuCertification);
        if (insert == 1)
            return RespBean.success("用户认证插入成功");
        else
            return RespBean.error("用户认证插入失败");
    }

    /**
     * 查询学生认证信息
     * @param userId
     * @return
     */
    @Override
    public StuCertification getStuInfo(Integer userId) {
        return stuCertificationMapper.selectOne(new QueryWrapper<StuCertification>().eq("user_id",userId));
    }

    /**
     * 学生头像上传
     * @param file
     * @return
     */
    @Override
    public RespBean uploadStuFace(MultipartFile file) throws Exception {
        if (file.isEmpty())
            return RespBean.error("文件为空！请重新选择文件");
        String filename = files.upload(file);
        UserInfo userInfo = (UserInfo) (SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        String url = ResourceUtils.getFile("classpath:") + "/static/image/" + filename;
        HashMap<String, Object> hashMap = new HashMap<>();
        //将路径存入数据库
        int updateURL = stuCertificationMapper.updateStuURL(url, userInfo.getUserId());
        if (updateURL == 1) {
            hashMap.put("msg","学生头像上传成功");
            hashMap.put("url",url);
            return RespBean.success("学生头像上传成功",hashMap);
        }else {
            hashMap.put("msg", "学生头像上传失败");
            hashMap.put("url", url);
            return RespBean.success("学生头像存储成功", hashMap);
        }
    }
}
